﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Xml.Linq;

namespace ThiTracNghiem
{
    public partial class formThiTracNghiem : Form
    {
        public formThiTracNghiem()
        {
            InitializeComponent();
        }

        private ArrayList arrCauHoi = new ArrayList();
        private ArrayList arrRbt = new ArrayList();
        private ArrayList arrTextbox = new ArrayList();
        private int cauhientai;
        private long thoigianthi;
        private Color mau1, mau2, mau3;
        
        private void formThiTracNghiem_Load(object sender, EventArgs e)
        {
            arrRbt.Add(RbtnA);
            arrRbt.Add(RbtnB);
            arrRbt.Add(RbtnC);
            arrRbt.Add(RbtnD);

            arrTextbox.Add(textBoxA);
            arrTextbox.Add(textBoxB);
            arrTextbox.Add(textBoxC);
            arrTextbox.Add(textBoxD);

            mau1 = mau2 = mau3 = Color.White;

            foreach (RadioButton rbt in arrRbt)
                rbt.Enabled = false;

            buttonShow.Enabled = false;
            buttonStop.Enabled = false;
            buttonPreview.Enabled = false;
            buttonNext.Enabled = false;
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            if (f.ShowDialog() == DialogResult.OK)
                textBoxSrc.Text = f.FileName;
        }

        private void ShowCauHoi()
        {
            CCauTracNghiem cauhoi = (CCauTracNghiem)arrCauHoi[cauhientai];
            label5.Text = (cauhientai + 1).ToString();
            textBoxNoiDung.Text = cauhoi.NoiDung;
            
            textBoxA.Text = cauhoi.DapAnA;
            textBoxB.Text = cauhoi.DapAnB;
            textBoxC.Text = cauhoi.DapAnC;
            textBoxD.Text = cauhoi.DapAnD;

            foreach (RadioButton rbt in arrRbt)
            {
                rbt.Checked = false;
                if (thoigianthi != 0)
                    rbt.Enabled = true;
                if (int.Parse(rbt.Tag.ToString()) == cauhoi.DapAnChon)
                    rbt.Checked = true;
            }

            foreach (TextBox txt in arrTextbox)
            {
                txt.BackColor = Color.White;
                if (int.Parse(txt.Tag.ToString()) == cauhoi.DapAnChon )
                    txt.BackColor = mau1;
                if (int.Parse(txt.Tag.ToString()) == cauhoi.DapAnDung)
                    txt.BackColor = mau2;
                if (int.Parse(txt.Tag.ToString()) == cauhoi.DapAnChon && int.Parse(txt.Tag.ToString()) == cauhoi.DapAnDung)
                    txt.BackColor = mau3;
            }

            buttonPreview.Enabled = true;
            buttonNext.Enabled = true;
            if (cauhientai == 0)
                buttonPreview.Enabled = false;
            if (cauhientai == arrCauHoi.Count-1)
                buttonNext.Enabled = false;
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            try
            {
                string src = textBoxSrc.Text;
                XDocument doc = new XDocument();
                doc = XDocument.Load(src);
                thoigianthi = long.Parse(doc.Element("DanhSach").Attribute("ThoiGian").Value)*60;
                var dscauhoi = from ch in doc.Element("DanhSach").Elements("CauHoi")
                               select ch;

                foreach (var ch in dscauhoi)
                {
                    CCauTracNghiem cauhoi = new CCauTracNghiem();
                    cauhoi.NoiDung = ch.Element("NoiDung").Value;
                    cauhoi.DapAnA = ch.Element("DapAnA").Value;
                    cauhoi.DapAnB = ch.Element("DapAnB").Value;
                    cauhoi.DapAnC = ch.Element("DapAnC").Value;
                    cauhoi.DapAnD = ch.Element("DapAnD").Value;
                    cauhoi.DapAnDung = int.Parse(ch.Element("DapAnDung").Value);
                    cauhoi.DapAnChon = 0;
                    arrCauHoi.Add(cauhoi);
                }

                cauhientai = 0;
                ShowCauHoi();
                timerThoiGianThi.Start();
                buttonStart.Enabled = false;
                buttonBrowse.Enabled = false;
                buttonStop.Enabled = true;
                textBoxSrc.ReadOnly = true;
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi đường dẫn đề thi");
            }
        }

        private void DapAnChon(object sender, EventArgs e)
        {
            CCauTracNghiem cauhoi = (CCauTracNghiem)arrCauHoi[cauhientai];
            foreach (RadioButton rbt in arrRbt)
                if (rbt.Checked == true)
                    cauhoi.DapAnChon = int.Parse(rbt.Tag.ToString());
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            cauhientai++;
            ShowCauHoi();
        }

        private void buttonPreview_Click(object sender, EventArgs e)
        {
            cauhientai--;
            ShowCauHoi();
        }

        private void ThoiGian(long giay)
        {
            string textthoigian = string.Format("{0} : {1}", giay / 60, giay % 60);
            label4.Text = textthoigian;
        }

        private void ChamDiem()
        {
            int diem = 0;
            foreach (CCauTracNghiem cauhoi in arrCauHoi)
            {
                if (cauhoi.DapAnChon == cauhoi.DapAnDung)
                    diem++;
            }
            string thongbao = string.Format("Số câu bạn làm đúng :{0}/{1}", diem, arrCauHoi.Count);
            MessageBox.Show(thongbao);
            label7.Text = diem.ToString() + "/" +arrCauHoi.Count.ToString();
        }

        private void KetThuc()
        {
            thoigianthi = 0;
            ChamDiem();
            foreach (RadioButton rbt in arrRbt)
                rbt.Enabled = false;

            buttonStop.Enabled = false;
            buttonShow.Enabled = true;
        }

        private void timerThoiGianThi_Tick(object sender, EventArgs e)
        {
            ThoiGian(thoigianthi);
            if (thoigianthi == 0)
            {
                timerThoiGianThi.Stop();
                MessageBox.Show("Hết giờ thi","Thông báo");
                KetThuc();
            }
            else
                thoigianthi--;
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            timerThoiGianThi.Stop();
            if (MessageBox.Show("Bạn có chắc kết thúc bài thi?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                MessageBox.Show("Bạn đã kết thúc bài thi", "Thông báo");
                KetThuc();
            }
            else
                timerThoiGianThi.Start();
        }

        private void buttonShow_Click(object sender, EventArgs e)
        {
            mau1 = Color.Red;
            mau2 = Color.Blue;
            mau3 = Color.Green;
            ShowCauHoi();
        }

        private void buttonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
